package com.onluy.frameworkcloud.base;

import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.bean.Pagination;
import com.onluy.frameworkcloud.data.entity.BaseEntity;
import com.onluy.frameworkcloud.mvc.dao.BaseSqlSessionDao;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class AppWalkService {

    @Autowired
    private BaseSqlSessionDao sqlSessionDao;


    public Pagination FormatPagination(ParamPagination paramPagination) {
        Pagination pagination = new Pagination();
        int pageSize = paramPagination.getPageSize();
        int currPage = paramPagination.getCurrPage();
        int start = (currPage - 1) * pageSize;
        start = start < 0 ? 0 : start;
        pagination.setRange(start, pageSize);
        pagination.setCurrPage(currPage);
        return pagination;
    }

    public BaseSqlSessionDao getSqlSessionDao() {
        return sqlSessionDao;
    }
    public void setSqlSessionDao(BaseSqlSessionDao sqlSessionDao) {
        this.sqlSessionDao = sqlSessionDao;
    }



    public BaseSqlSessionDao dao(){
        return sqlSessionDao;
    }

    public <T>T model(BaseEntity entity) {
        return dao().selectOne(entity );
    };
    public  int add(BaseEntity entity){
        return dao().insert(entity);
    };
    public  int update(BaseEntity entity){
        return  dao().update(entity);
    };
    public  int del(BaseEntity entity){
        return dao().delete(entity);
    };
    public <T> PageData<T> list(String statementId, Object param, Pagination pagination){
        return dao().selectList(statementId,param,pagination);
    };
    public   <T> PageData<T> list(BaseEntity entity, Pagination pagination){
        return dao().selectList(entity,pagination);
    };

    public <T> List<T> list(BaseEntity entity){
        return dao().selectList(entity);
    };

    public  <T> List<T> list(String statementId, Object param){
        return dao().selectList(statementId,param);
    };


}
